﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using System.IO;
using DigiLib;

public partial class Administrator_Controls_ModuleTypesEdit : Admin_Controls_Base {
    protected void Page_Load(object sender, EventArgs e) {
        base.removeHGC(divMessage);
        if (!IsPostBack) {
            base.bindDDL(ddlContent, BO_portal_content_types.SelectAll(1), "DESCRIPTION", "CONTENT_TYPE_ID");
            loadData();
        }
    }
    private void loadData() {
        ObjDT = BO_portal_module_types.Search(Id);
        txtName.Text = ObjDT.Rows[0]["TYPE_NAME"].ToString();
        txtDescription.Text = ObjDT.Rows[0]["TYPE_DESCRIPTION"].ToString();
        cboOption.Checked = Convert.ToBoolean(ObjDT.Rows[0]["TYPE_OPTION"]);
        ctThumb.Src = "/" + Setting.PathAdmin + ObjDT.Rows[0]["THUMB_IMAGE"].ToString();
        ctLocation.Src = "/" + Setting.PathAdmin + ObjDT.Rows[0]["PICTURE_LOCATION"].ToString();
        ddlContent.SelectedValue = ObjDT.Rows[0]["CONTENT_TYPE_ID"].ToString();
    }
    protected void btnSubmit_Click(object sender, EventArgs e) {
        try {
            ObjDT = BO_portal_module_types.Search(Id);
            string name = txtName.Text.Trim();
            string description = txtDescription.Text.Trim();
            int content = Convert.ToInt32(ddlContent.SelectedValue);
            bool option = cboOption.Checked;
            string thumb = base.Upload("moduletypes", string.Empty, fupThunb, divMessage, true);
            string location = base.Upload("moduletypes", string.Empty, fupLocation, divMessage, true);
            string strResult = BO_portal_module_types.Update(Id, name,
                                                                description,
                                                                option,
                                                                thumb,
                                                                location,
                                                                content,
                                                                DateTime.Now,
                                                                Administrator.USER_NAME,
                                                                DateTime.Now,
                                                                Administrator.USER_NAME,
                                                                true);
            if (strResult != "0") {
                divMessage.InnerHtml = MessageBox.Success("Hệ Thống", "Cập nhật thành công!");
                Javascripts.AlertRedirect(this.Page, "Cập nhật thành công!", "default.aspx?control=ModuleTypes");
            } else
                divMessage.InnerHtml = MessageBox.Error("Hệ Thống", "Có lỗi trong quá trình xử lý, thử lại sau!");
        } catch (Exception ex) {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
        }
    }
}
